// pages/order/order.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['全部', '已完成', '待付款', '待完成', '售后/退款'],
    currentTab: 0,
    list: [],
    page: 1,
    page_size: 5,
    no_data: 0,
    onearth: false, // 是否已经到达底部 
    have: true,
  },
  //响应点击导航栏
  navbarTap: function(e) {
    let that = this;
    that.setData({
      list: [],
      page: 1,
      page_size: 5,
      no_data: 0,
      onearth: false,
      have: true,
      currentTab: e.currentTarget.dataset.idx
    });
    if (e.currentTarget.dataset.idx == 0) {
      that.lists()
    } else if (e.currentTarget.dataset.idx == 1) {
      that.lists(3)
    } else if (e.currentTarget.dataset.idx == 2) {
      that.lists(1)
    } else if (e.currentTarget.dataset.idx == 3) {
      that.lists(2)
    } else {
      that.lists(5)
    }
  },
  ordType(e) {
    console.log(e.currentTarget.dataset)
    // 跳转到拼团的详情或者其他普通类型的
    if (e.currentTarget.dataset.status == 6 || e.currentTarget.dataset.status == 7) {
      wx.navigateTo({
        url: '../orderDetail/refund?order_id=' + e.currentTarget.dataset.oid,
      })
    } else {
      wx.navigateTo({
        url: '../orderDetail/ordType?order_id=' + e.currentTarget.dataset.oid,
      })
    }
  },
  // 获取我的订单列表
  lists(status) {
    let that = this;
    let page_size = this.data.page_size,
      no_data = this.data.no_data,
      page = this.data.page;
    if (no_data == 0) {
      wh.Post('/api/mall.order/mylist', {
        status: status,
        page: page,
        page_num: page_size,
      }, function(res) {
        console.log('订单列表', res)
        let shop = that.data.list.concat(res.list),
          count = res.list.length;
        if (page_size > count) {
          that.data.no_data = 1;
          if (shop.length > 2) {
            that.setData({
              onearth: true
            })
          }
        }
        that.setData({
          list: shop
        })
        if (that.data.list.length == 0) {
          that.setData({
            have: false,
          });
        } else {
          that.setData({
            have: true,
          });
        }
      })
    }
  },

  // 提醒发货
  remind(e) {
    console.log(e.currentTarget.dataset.oid)
    let that = this;
    wh.Post('/api/mall.order/remind', {
      order_id: e.currentTarget.dataset.oid
    }, function(res) {
      wx.showToast({
        title: '提醒成功',
        icon: 'none',
        duration: 2000,
      });
    })
  },
  // 取消订单
  cancel(e) {
    let that = this,
      list = this.data.list,
      order_id = e.currentTarget.dataset.oid,
      inx = e.currentTarget.dataset.inx;
    wx.showModal({
      title: '',
      content: '是否取消该订单',
      success: function(res) {
        if (res.confirm) {
          wh.Post('/api/mall.order/cancel', {
            order_id: order_id
          }, function(res) {
            that.lists()
            wx.showToast({
              title: '取消成功',
            });
          })
          let arr = that.data.list;
          arr.splice(inx, 1)
          that.setData({
            list: arr
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 删除订单
  del_order(e) {
    let that = this,
      list = this.data.list,
      order_id = e.currentTarget.dataset.oid,
      inx = e.currentTarget.dataset.inx;
    wx.showModal({
      title: '',
      content: '是否删除该订单',
      success: function (res) {
        if (res.confirm) {
          wh.Post('/api/mall.order/del', {
            order_id: order_id
          }, function (res) {
            wx.showToast({
              title: '删除成功',
            });
          })
          let arr = that.data.list;
          arr.splice(inx, 1)
          that.setData({
            list: arr
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 取消申请退款
  cancel_refund(e){
    let that = this,
      list = this.data.list,
      order_id = e.currentTarget.dataset.oid,
      inx = e.currentTarget.dataset.inx;
    wx.showModal({
      title: '',
      content: '是否取消申请退款',
      success: function (res) {
        if (res.confirm) {
          wh.Post('/api/mall.order/cancel_refund', {
            order_id: order_id
          }, function (res) {
            wx.showToast({
              title: '取消成功',
            });
          })
          let arr = that.data.list;
          arr.splice(inx, 1)
          that.setData({
            list: arr
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 订单确认收货
  confirm(e){
    let that = this,
      list = this.data.list,
      order_id = e.currentTarget.dataset.oid,
      inx = e.currentTarget.dataset.inx;
    wx.showModal({
      title: '',
      content: '是否确认收货',
      success: function (res) {
        if (res.confirm) {
          wh.Post('/api/mall.order/confirm', {
            order_id: order_id
          }, function (res) {
            wx.showToast({
              title: '收货成功',
            });
          })
          let arr = that.data.list;
          arr.splice(inx, 1)
          that.setData({
            list: arr
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }, 
  // 开始支付及支付后的回调
  startPay(e) {
    console.log('订单号', e.currentTarget.dataset.oid)
    let that = this;
    wh.Post('/api/mall.order/payment', {
      order_id: e.currentTarget.dataset.oid
    }, function (res) {
      console.log('支付信息', res)
      // 微信支付
      wx.requestPayment({
        'timeStamp': res.timeStamp + '',
        'nonceStr': res.nonceStr,
        'package': res.package,
        'signType': res.signType,
        'paySign': res.paySign,
        'success': function (ref) {
          console.log('支付成功', ref)
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 2000,
            complete: function () {
              wx.reLaunch({
                url: '../paysuss/paysuss',
              })
            }
          })
        },
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {   
    let c = parseInt(options.c) + parseInt('1')
    if (c == 0) {
      this.lists()
    } else if (c == 1) {
      this.lists(3)
    } else if (c == 2) {
      this.lists(1)
    } else if (c == 3) {
      this.lists(2)
    } else {
      this.lists(5)
    }
    console.log(c)
    this.setData({
      currentTab: c
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let no_data = this.data.no_data,
      page = this.data.page,
      currentTab = this.data.currentTab,
      page_size = this.data.page_size;
    if (no_data == 0) {
      this.data.page = page + 1;
      if (currentTab == 0) {
        this.lists()
      } else if (currentTab == 1) {
        this.lists(3)
      } else if (currentTab == 2) {
        this.lists(1)
      } else if (currentTab == 3) {
        this.lists(2)
      } else {
        this.lists(5)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})